package com.cn.xiaonuo.modular.controller.app;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.xiaonuo.core.pojo.response.ResponseData;
import com.cn.xiaonuo.core.util.DateUtils;
import com.cn.xiaonuo.core.util.StringUtils;
import com.cn.xiaonuo.modular.model.app.GroupInfo;
import com.cn.xiaonuo.modular.model.app.GroupMsg;
import com.cn.xiaonuo.modular.service.app.GroupService;

/***
 * 
 * 群（读书会）控制器
 *
 */
@Controller
@RequestMapping("/group")
public class GroupController {
	
	@Autowired
	private GroupService groupService;
	
	/**
	 * 查询读书会列表
	 * @param userId
	 * @return
	 */
	@ResponseBody
    @PostMapping("/findGroupList")
	public ResponseData findGroupList(Long userId) {
		if(null == userId) {
    		return ResponseData.error("userId不能为空！");
    	}
		return ResponseData.success(groupService.findGroupList(userId));
	}
	
	/** 创建读书会 */
	@ResponseBody
    @PostMapping("/createGroupInfo")
	public ResponseData createGroupInfo(@RequestBody GroupInfo info) {
		if(null == info) {
			return ResponseData.error("参数不能为空！");
		}
		if(null == info.getGroupUserId()) {
			return ResponseData.error("groupUserId不能为空！");
		}
		if(StringUtils.isEmpty(info.getGroupName())) {
			return ResponseData.error("groupName不能为空！");
		}
		info.setCreateTime(DateUtils.getDate());
		groupService.createGroupInfo(info);
		return ResponseData.success();
	}
	
	/** 修改读书会 */
	@ResponseBody
    @PostMapping("/updateGroupInfo")
	public ResponseData updateGroupInfo(@RequestBody GroupInfo info) {
		if(null == info) {
			return ResponseData.error("参数不能为空！");
		}
		if(null == info.getGroupId()) {
			return ResponseData.error("groupId不能为空！");
		}
		if(null == info.getGroupUserId()) {
			return ResponseData.error("groupUserId不能为空！");
		}
		if(StringUtils.isEmpty(info.getGroupName())) {
			return ResponseData.error("groupName不能为空！");
		}
		groupService.updateGroupInfo(info);
		return ResponseData.success();
	}
	
	/** 删除（解散）读书会 */
	@ResponseBody
    @PostMapping("/deleteGroupInfo")
	public ResponseData deleteGroupInfo(Long userId,Long groupId) {
		if(null == groupId) {
			return ResponseData.error("groupId不能为空！");
		}
		if(null == userId) {
			return ResponseData.error("userId不能为空！");
		}
		groupService.deleteGroupInfo(userId, groupId);
		return ResponseData.success();
	}
	
	/** 申请加入读书会 */
	@ResponseBody
    @PostMapping("/addGroupApply")
	public ResponseData addGroupApply(Long groupId,Long userId) {
		if(null == groupId) {
			return ResponseData.error("groupId不能为空！");
		}
		if(null == userId) {
			return ResponseData.error("userId不能为空！");
		}
		groupService.addGroupApply(groupId, userId);
		return ResponseData.success();
	}
	
	/** 查询消息通知 */
	@ResponseBody
    @PostMapping("/findGroupMsgList")
	public ResponseData findGroupMsgList(Long userId,Integer pageNo,Integer pageSize) {
		if(null == userId) {
    		return ResponseData.error("userId不能为空！");
    	}
    	if(null == pageNo) {
    		return ResponseData.error("pageNo不能为空！");
    	}
    	if(null == pageSize) {
    		return ResponseData.error("pageSize不能为空！");
    	}
    	List<GroupMsg> findGroupMsgList = groupService.findGroupMsgList(userId, pageNo, pageSize);
    	return ResponseData.success(findGroupMsgList);
	}
	
	/** 操作消息通知 */
	@ResponseBody
    @PostMapping("/opGroupMsg")
	public ResponseData opGroupMsg(Long msgId,String opType) {
		if(null == msgId) {
    		return ResponseData.error("msgId不能为空！");
    	}
    	if(null == opType) {
    		return ResponseData.error("opType不能为空！");
    	}
    	if(!"1".equals(opType) && !"-1".equals(opType)) {
    		return ResponseData.error("opType只能为1，-1！");
    	}
    	groupService.opGroupMsg(msgId, opType);
    	return ResponseData.success();
	}
	
	/** 操作申请加入通知 */
	@ResponseBody
    @PostMapping("/opGroupApply")
	public ResponseData opGroupApply(Long msgId,String groupApplyId,String opType) {
		if(null == msgId) {
    		return ResponseData.error("msgId不能为空！");
    	}
    	if(null == opType) {
    		return ResponseData.error("opType不能为空！");
    	}
    	if(!"2".equals(opType) && !"3".equals(opType)) {//2：同意申请通知，3：拒绝申请通知
    		return ResponseData.error("opType只能为2，3！");
    	}
    	groupService.opGroupApply(msgId,groupApplyId,opType);
		return ResponseData.success();
	}
	
	@ResponseBody
    @PostMapping("/searchGroupList")
	public ResponseData searchGroupList(@RequestBody GroupInfo info,Integer pageNo,Integer pageSize) {
    	if(null == pageNo) {
    		return ResponseData.error("pageNo不能为空！");
    	}
    	if(null == pageSize) {
    		return ResponseData.error("pageSize不能为空！");
    	}
		return ResponseData.success(groupService.searchGroupList(info, pageNo, pageSize));
	}
	
	
	
	

}
